package com.example.springtransaction.controller;

import com.example.springtransaction.model.LogInfo;
import com.example.springtransaction.model.UserInfo;
import com.example.springtransaction.service.LogInfoService;
import com.example.springtransaction.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    // 数据源事务管理器
    @Autowired
    private DataSourceTransactionManager transactionManager;

    // 事务的定义
    @Autowired
    private TransactionDefinition transactionDefinition;

    @RequestMapping(value = "/adduser")
    public int addUser(UserInfo userInfo){
        // 验证 用户名和密码 不能为空
        if( userInfo == null
                || !StringUtils.hasLength(userInfo.getUsername())
                || !StringUtils.hasLength(userInfo.getPassword())){
            return 0;
        }
        // 开启(获取)事务
        TransactionStatus transactionStatus = transactionManager.getTransaction(transactionDefinition);
        int ret = userService.addUser(userInfo);
        System.out.println("addUser 受到影响的行数：" + ret);
        // 提交事务 / 回滚事务

        // 回滚事务
        transactionManager.rollback(transactionStatus);

        // 提交事务
//        transactionManager.commit(transactionStatus);
        return ret;
    }

    @RequestMapping(value = "/adduser2")
    @Transactional
    public int addUser2(UserInfo userInfo){
        // 验证 用户名和密码 不能为空
        if( userInfo == null
                || !StringUtils.hasLength(userInfo.getUsername())
                || !StringUtils.hasLength(userInfo.getPassword())){
            return 0;
        }
        int ret = userService.addUser(userInfo);
        System.out.println("addUser 受到影响的行数：" + ret);
        return ret;
    }
    @RequestMapping(value = "/adduser3")
    @Transactional
    public int addUser3(UserInfo userInfo){
        // 验证 用户名和密码 不能为空
        if( userInfo == null
                || !StringUtils.hasLength(userInfo.getUsername())
                || !StringUtils.hasLength(userInfo.getPassword())){
            return 0;
        }
        int ret = userService.addUser(userInfo);
        System.out.println("addUser 受到影响的行数：" + ret);
        try {
            int num = 10 / 0 ;
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return ret;
    }

    @Autowired
    private LogInfoService logInfoService;

    // 虽然事务的传播级别默认是 REQUIRED, 但是显示写的话，你们更清楚。
    @Transactional
    @RequestMapping(value = "adduser4")
    public int addUser4(UserInfo userInfo){
        // 验证 用户名和密码 不能为空
        if( userInfo == null
                || !StringUtils.hasLength(userInfo.getUsername())
                || !StringUtils.hasLength(userInfo.getPassword())){
            return 0;
        }
        int userResult = userService.addUser(userInfo);
        System.out.println("添加用户：" + userResult );

        int logResult = 0;
        LogInfo logInfo = new LogInfo();
        logInfo.setName("添加用户");
        logInfo.setDesc("添加用户的结果：" + logResult);
        logInfoService.addLog(logInfo);
        return userResult;
    }

    @Transactional
    @RequestMapping(value = "adduser5")
    public int addUser5(UserInfo userInfo){
        // 验证 用户名和密码 不能为空
        if( userInfo == null
                || !StringUtils.hasLength(userInfo.getUsername())
                || !StringUtils.hasLength(userInfo.getPassword())){
            return 0;
        }
        int userResult = userService.addUser(userInfo);
        System.out.println("添加用户：" + userResult );

        int logResult = 0;
        LogInfo logInfo = new LogInfo();
        logInfo.setName("添加用户");
        logInfo.setDesc("添加用户的结果：" + logResult);
        logInfoService.addLog(logInfo);
        return userResult;
    }
}

